﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 表示MongoDb的组织机构提供器
    /// </summary>
    public sealed class MongoDbOrganizeProvider : MongoDBPhysicalModelProvider<Organization>, IOrganizeProvider
    {
        /// <summary>
        /// 创建新的组织机构实体
        /// </summary>
        /// <returns>
        /// 创建的实体
        /// </returns>
        public override Organization Create()
        {
            return new Organization()
            {
                Name = "未命名"
            };
        }

        /// <summary>
        /// 取得组织机构列表
        /// </summary>
        /// <param name="parentOrganize">
        /// 该组织机构的父级组织机构
        /// </param>
        /// <returns>
        /// 取得的组织机构
        /// </returns>
        public List<Organization> GetList(Guid? parentOrganize = null)
        {
            IMongoCollection<Organization> col = GetCollection();
            FilterDefinition<Organization> filter = (FilterDefinition<Organization>)(m => m.ParentOrganization == parentOrganize);
            return col.GetAll(filter);
        }
        /// <summary>
        /// 重新设置指定组织机构的父组织机构Id
        /// </summary>
        /// <param name="orgId">
        /// 要设置的组织机构ID
        /// </param>
        /// <param name="parId">
        /// 要设置的父组织机构ID
        /// </param>
        /// <returns>
        /// 一个表示是否设置成功的值
        /// </returns>
        public bool SetParent(Guid orgId, Guid? parId)
        {
            UpdateDefinition<Organization> updefine = Builders<Organization>.Update.Set(m => m.ParentOrganization, parId);
            return Update(orgId, updefine);
        }
        /// <summary>
        /// 重命名指定组织机构
        /// </summary>
        /// <param name="orgId">
        /// 要重命名的组织机构Id
        /// </param>
        /// <param name="newName">
        /// 要重命名的组织机构的新名称
        /// </param>
        /// <returns>
        /// 一个表示是否重命名成功的值
        /// </returns>
        public bool Rename(Guid orgId, string newName)
        {
            UpdateDefinition<Organization> updefine = Builders<Organization>.Update.Set(m => m.Name, newName);
            return Update(orgId, updefine);
        }
        /// <summary>
        /// 更新指定组织机构的信息
        /// </summary>
        /// <param name="orgId">
        /// 组织机构ID
        /// </param>
        /// <param name="name">
        /// 组织机构名称
        /// </param>
        /// <param name="type">
        /// 组织机构类型
        /// </param>
        /// <param name="leaders">
        /// 组织机构领导
        /// </param>
        /// <param name="chargeLeaders">
        /// 组织机构分管领导
        /// </param>
        /// <param name="locked">
        /// 组织机构是否被锁定
        /// </param>
        /// <returns>
        /// 更新结果
        /// </returns>
        public bool Update(Guid orgId, string name = null, string type = null, string leaders = null, string chargeLeaders = null, bool? locked = null)
        {
            UpdateDefinition<Organization> upDef = null;
            if (name != null)
            {
                upDef = BuildUpdateDef(m => m.Name, name, upDef);
            }
            if (type != null)
            {
                upDef = BuildUpdateDef(m => m.OrganizationType, type, upDef);
            }
            if (leaders != null)
            {
                upDef = BuildUpdateDef(m => m.Leaders, leaders, upDef);
            }
            if (chargeLeaders != null)
            {
                upDef = BuildUpdateDef(m => m.ChargeLeaders, chargeLeaders, upDef);
            }
            if (locked != null)
            {
                upDef = BuildUpdateDef(m => m.Locked, (bool)locked, upDef);
            }

            if (upDef == null)
            {
                return true;
            }
            else
            {
                return Update(orgId, upDef);
            }
        }
        /// <summary>
        /// 查询名称中包含指定字符的组织机构集合
        /// </summary>
        /// <param name="nameLike">
        /// 要查询的组织机构名称
        /// </param>
        /// <param name="skip">
        /// 返回数据的起始位置
        /// </param>
        /// <param name="limit">
        /// 限制数量
        /// </param>
        /// <returns>
        /// 查询结果
        /// </returns>
        public List<Organization> QueryByName(string nameLike, int skip = 0, int limit = 5)
        {
            IMongoCollection<Organization> col = GetCollection();
            FindOptions<Organization> findOpt = new FindOptions<Organization>()
            {
                Skip = skip,
                Limit = limit
            };
            return col.QueryByName(nameLike, findOpt);
        }
    }
}
